We’re going to look at the Instacart data

library(tidyverse)
library(p8105.datasets)
library(plotly)
data("instacart")

instacart_df = instacart

instacart_df =
  instacart %>% 
  mutate(
    department = as.factor(department),
    aisle = as.factor(aisle),
    order_dow = factor(order_dow,
                       levels = 0:6,
                       labels = c("Sun","Mon","Tue","Wed","Thu","Fri","Sat"))) %>%
  sample_n(1000)

Bar Plot

instacart_bar =
instacart_df %>%
  count(department) %>%
  mutate(department = fct_reorder(department, n)) %>%
  plot_ly(
    x = ~department,
    y = ~n,
    color = ~department,
    type = "bar",
    colors = "viridis") %>%
  layout(
    title = "Most Frequently Ordered Departments",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Number of Products Ordered")
  )

instacart_bar

Box Plot

instacart_box = 
  instacart %>%
  filter(department %in% c("produce", "snacks", "dairy eggs", "frozen", "beverages")) %>%
  mutate(department = fct_reorder(department, order_hour_of_day)) %>% 
  plot_ly(
    x = ~department,
    y = ~order_hour_of_day,
    color = ~department,
    type = "box",
    colors = "viridis") %>% 
  layout(
    title = "Distribution of Order Hour by Top 5 Departments",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Hour of Day Order was Placed")
  )

instacart_box

Line Plot

instacart_line =
  instacart_df %>%
  group_by(order_dow) %>%                              
  summarise(avg_hour = mean(order_hour_of_day, na.rm = TRUE)) %>%  
  plot_ly(
    x = ~order_dow,
    y = ~avg_hour,
    type = "scatter",
    mode = "lines+markers",
    line = list(color = "black"),
    marker = list(size = 8)
  ) %>%
  layout(
    title = "Average Order Hour by Day of Week",
    xaxis = list(title = "Day of Week"),
    yaxis = list(title = "Average Hour of Day (0–23)"))

instacart_line